<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ModulesAdmin
 *
 * @author Костя
 */
class ModulesAdmin {

    private $_db;
    private $_fld;
    public $groups = Array('system' => 'Системные модули', 'standart' => 'Дополнительные модули', 'other' => 'Остальные модули');

    public function __construct() {
        $this->_db = Db::getInstance();
        $this->_fld = new Files($_SERVER['DOCUMENT_ROOT'] . '/includes/modules');
    }

    public function getAllModules($type = "fronend") {
        $folders = $this->_fld->lld();
        //print_r($folders);
        $module = Array();
        foreach ($folders as $folder => $data) {

            $name = str_replace($_SERVER['DOCUMENT_ROOT'] . '/includes/modules/', '', $folder);
            $file = $this->_fld->read_file($_SERVER['DOCUMENT_ROOT'] . '/includes/modules/' . $name . '/' . $name . '.php', true, 1024, 3, 10);
            if ($file === false) {
                $module[$name] = Array('error' => 'File not Found');
            } else {
                $module[$name] = Array(
                    'main_file' => '/includes/modules/' . $name . '/' . $name . '.php',
                    'author' => $this->getInfo('author', $file[0]),
                    'version' => $this->getInfo('version', $file[2]),
                    'name' => $this->getInfo('name', $file[1]),
                    'licence' => $this->getInfo('licence', $file[3]),
                    'minimum-q-cms-version' => $this->getInfo('minimum-q-cms-version', $file[4]),
                    'group' => $this->getInfo('group', $file[6]),
                    'descr' => $this->getInfo('descr', $file[5]),
                );
                $group = str_replace('new ', '', $this->getInfo('group', $file[6]));
                if (!array_key_exists($group, $this->groups)) {
                    $this->groups[strtolower($group)] = $group;
                    //strpo
                    //echo 11;
                }
                
                $this->groups[($group != '')?strtolower($group) . '_mod':'other_mod'][] = $name;
            }
        }
        //print_r($module);
        return $module;
    }

    /*
     * @TODO: Перестроить так чтобы её небыло!!
     */

    private function getInfo($field, $field_value) {
        return str_replace($field . '=', '', trim($field_value));
    }

    private function registerAgain() {
        //Check all modules and set exec priority!
        //write modules priority to DB
    }

}

?>
